﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Library.Facade.SQLServer;
using System.Configuration;
using Web.Library.DomainObject.SQLServer;

using Web.Support;

/// <summary>
/// Summary description for Authorization
/// </summary>

namespace Web.Support
{
    public class Authorization : BasePage
    {
        private SysMenuFacade FacadeMenu = new SysMenuFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        private SysGroupAuthorizationFacade FacadeAuth = new SysGroupAuthorizationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        public Authorization()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string IsAuthorized(string pathDomain, bool isAuthenticated)
        {
            string feedback = string.Empty;
            //FB.DomainLayer.Menu menu = new FB.DomainLayer.Menu();
            //menu = new MenuFacade().RetrieveMenuByUrl(pathDomain);

            SysMenu menu = new SysMenu();
            menu = FacadeMenu.GetByUrl(pathDomain);

            if (menu != null)
            {
                if (menu.MenuID > 0)
                {
                    if (!isAuthenticated)
                    {
                        feedback = "loginUrl";
                    }
                    else
                    {
                        //FB.DomainLayer.GroupAuthorization authorization = new FB.DomainLayer.GroupAuthorization();
                        //authorization = new GroupAuthorizationFacade().RetrieveAuthorizationByGroupIDAndMenuID(new FB.DomainLayer.AuthInfo().UserInfo().GroupID, menu.ID);

                        SysGroupAuthorization auth = new SysGroupAuthorization();
                        auth = FacadeAuth.RetrieveAuthorizationByGroupIDAndMenuID(base.GroupID, menu.MenuID);

                        if (auth != null)
                        {
                            if (auth.AuthID > 0)
                            {
                                // do something here <Do not remove this section>
                            }
                            else
                            {
                                feedback = "unauthorizedUrl";
                            }
                        }
                        else
                        {
                            feedback = "unauthorizedUrl";
                        }
                    }
                }
            }
            return feedback;
        }
    }
}